import os
from xml.dom import minidom

import xlwt
import xlrd
import os.path
import xml.dom.minidom


# 获取string.xml路径
def getStringPath(basePath):
    array = {}
    pathList = os.listdir(basePath)
    for name in pathList:
        nameArray = name.split("-")
        if nameArray[0] == "values":
            path = basePath + "\\" + name + "\\strings.xml"
            array[name] = path
    return array


# 将string.xml的内容转成json
def getStringJson(strPath):
    strJson = {}
    for key in strPath:
        value = strPath[key]
        if os.path.isfile(value):
            dom = xml.dom.minidom.parse(value)
            # 获取根元素
            root = dom.documentElement
            # 获取string列表
            resource = root.getElementsByTagName('string')
            for node in resource:
                id = node.getAttribute('name')
                data = {}
                bean = strJson.get(id)
                if bean is not None:
                    data = bean
                strValue = ""
                if node.firstChild is not None:
                    strValue = node.firstChild.data
                data['id'] = id
                data[key] = strValue
                strJson[id] = data
    return strJson


# 输出成文件
def outputXls(strPath, strJson):
    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('string', cell_overwrite_ok=True)
    ws.write(0, 0, "id")
    keyJson = {}
    topindex = 1
    for key in strPath:
        ws.write(0, topindex, key)
        keyJson[key] = topindex
        topindex = topindex + 1
    beanIndex = 1
    for key, value in strJson.items():
        ws.write(beanIndex, 0, key)
        for columnName, columnValue in keyJson.items():
            item = value.get(columnName)
            if item is not None:
                ws.write(beanIndex, int(columnValue), item)
        beanIndex += 1
    wb.save(r'out.xls')


# 读取到的数据转换成json
def readXls():
    book = xlrd.open_workbook(r'out.xls')
    rewData = book.sheet_by_index(0)
    listJson = {}
    topJson = {}
    topline = rewData.row_values(0)
    for index, bean in enumerate(topline):
        topJson[index] = bean

    for index in range(1, rewData.nrows):
        lineData = rewData.row_values(index)
        for indexJ in range(1, len(lineData)):
            key = topJson.get(indexJ)
            jsonStr = listJson.get(key)
            if jsonStr is None:
                listJson[key] = {}
            if lineData[0] is not None and lineData[indexJ] is not None:
                listJson.get(key)[lineData[0]] = lineData[indexJ]
    return listJson


def writeXml(jsonList):
    for key, value in jsonList.items():
        # 1.创建DOM树对象
        dom = minidom.Document()
        # 2.创建根节点。每次都要用DOM对象来创建任何节点。
        root_node = dom.createElement('resources')
        root_node.setAttribute('xmlns:tools', "http://schemas.android.com/tools")
        # 3.用DOM对象添加根节点
        dom.appendChild(root_node)
        # 添加string节点
        for child_key, child_value in value.items():
            child_node = dom.createElement('string')
            child_node.setAttribute("name", child_key)
            child_node.appendChild(dom.createTextNode(child_value))
            root_node.appendChild(child_node)

        # 保存文件
        with open(key + r'/string.xml', 'w', encoding='utf-8') as f:
            dom.writexml(f, addindent='\t', newl='\n', encoding='utf-8')
